﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
namespace Module_Basic_Entities
{
    public class AnimatedTile
    {
          public int type;
          public int delay;
          [ContentSerializerIgnore]
          public int delayCountdown;
          [ContentSerializerIgnore]
          public int currentFrameIndex;
          public int line;  
          public int column;
          public int repeatline;
          public int repeatcolumn;
          public List<int> frames;
    
         
          public  const int AN_NONE = 0;
          public  const int AN_LOOPF = 1;
          public  const int AN_LOOPR = 2;
          public  const int AN_ONCE = 3;
          public  const int AN_ONCES = 9;
          public  const int AN_ONCEH = 4;
          public  const int AN_PPFF = 5;
          public  const int AN_PPFR = 6;
          public  const int AN_PPRF = 7;
          public  const int AN_PPRR = 8;

          public AnimatedTile()
          {
          }

          public AnimatedTile(int type , int delay, int line, int column, List<int> frames)
          {
              this.type = type;
              this.delay = delay;
              this.line = line;
              this.column = column;
              this.frames = frames;
          }

          public int getCurrentFrameId()
          {
              return frames.ToArray()[currentFrameIndex];
          }

          public void updateAnimation()
  {
    this.delayCountdown -= 1;
    if (this.delayCountdown < 0) {
      this.delayCountdown = this.delay;

      switch (this.type) {
      case 0:
        break;
      case 1:
        this.currentFrameIndex += 1;
        if (this.currentFrameIndex < this.frames.Count) break;
        this.currentFrameIndex = 0; break;
      case 2:
        this.currentFrameIndex -= 1;
        if (this.currentFrameIndex >= 0) break;
        this.currentFrameIndex = (this.frames.Count - 1); break;
      case 3:
              
        this.currentFrameIndex += 1;
        if (this.currentFrameIndex < this.frames.Count) break;
        this.currentFrameIndex = 0;
        this.type = 9; break;
      case 9:
        break;
      case 4:
        if (this.currentFrameIndex >= this.frames.Count - 1) break;
        this.currentFrameIndex += 1; break;
      case 5:
        this.currentFrameIndex += 1;
        if (this.currentFrameIndex < this.frames.Count) break;
        this.currentFrameIndex -= 2;
        this.type = 6; break;
      case 6:
        this.currentFrameIndex -= 1;
        if (this.currentFrameIndex >= 0) break;
        this.currentFrameIndex += 2;
        this.type = 5; break;
      case 7:
        this.currentFrameIndex -= 1;
        if (this.currentFrameIndex >= 0) break;
        this.currentFrameIndex += 2;
        this.type = 8; break;
      case 8:
        this.currentFrameIndex += 1;
        if (this.currentFrameIndex < this.frames.Count) break;
        this.currentFrameIndex -= 2;
        this.type = 7; break;
      }
    }
  }

  public static bool Contains(int line, int column, AnimatedTile t)
  {
      return t.column == column && t.line == line;
  }
    }
}
